//
//  DABorrowBookTableViewCell.m
//  DABook
//
//  Created by DreamArts on 16/4/22.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DABorrowBookTableViewCell.h"
#import "DABorrowBookModel.h"
#import "DALabel.h"
#import "DAHeader.h"
#import "Utility.h"
#import "DAImageView.h"
#import "UIImageView+WebCache.h"
#import "DACommentButton.h"
#import "DACommentView.h"

@interface DABorrowBookTableViewCell ()
/** 用户头像*/
@property (weak, nonatomic) IBOutlet UIImageView *userImageView;
/** 用户名*/
@property (weak, nonatomic) IBOutlet DALabel *userNameLabel;
/** 时间轴*/
@property (weak, nonatomic) IBOutlet UILabel *timeLineLabel;
/** 状态图片*/
@property (weak, nonatomic) IBOutlet UIImageView *stateImageView;
/** 时间label*/
@property (weak, nonatomic) IBOutlet DALabel *timeLabel;
/** 图书的图片*/
@property (weak, nonatomic) IBOutlet UIImageView *bookImageView;
/** 出借人label*/
@property (weak, nonatomic) IBOutlet DALabel *lenderLabel;
/** 借阅期label*/
@property (weak, nonatomic) IBOutlet DALabel *borrowTimeLabel;
/** 图书的名字*/
@property (weak, nonatomic) IBOutlet DALabel *bookNameLabel;
/** 状态图片的背景view*/
@property (weak, nonatomic) IBOutlet UIView *stateView;

@property (nonatomic, strong) NSMutableArray *heightArray;


@end
@implementation DABorrowBookTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setTimelineModel:(DAHomePageTimeLineModel *)timelineModel{
    if (_timelineModel != timelineModel) {
        _timelineModel = timelineModel;
    }
    // 头像
   // [self.userImageView sd_setImageWithURL:[NSURL URLWithString:timelineModel.userFace]];
   [self.userImageView sd_setImageWithURL:[NSURL URLWithString:timelineModel.userFace] placeholderImage:[UIImage imageNamed:@"holderface"] options:SDWebImageRefreshCached];
    self.userImageView.layer.cornerRadius = 3;
    self.userImageView.clipsToBounds = YES;
    // 用户民
    NSString *userName = timelineModel.userName;
    [self.userNameLabel setText:userName fitToContent:YES];
    self.userNameLabel.textColor = [UIColor colorWithHexString:bookNameColor];
    // 时间轴颜色
    self.timeLineLabel.backgroundColor = [UIColor colorWithHexString:timeLineColor];
    // 时间
    NSString *borrow = NSLocalizedString(@"Borrow", nil);
    self.timeLabel.text = [NSString stringWithFormat:@"%@ %@", self.timelineModel.timelineDate, borrow];
    self.timeLabel.textColor = [UIColor colorWithHexString:timeColor];
    
    UIView *bookCover = [[UIView alloc]initWithFrame:CGRectMake(0 , 0, self.bookImageView.frame.size.width, self.bookImageView.frame.size.height)];
    bookCover.backgroundColor = [UIColor supportGreenColor];
    DALabel *bookCoverName = [[DALabel alloc]initWithFrame:CGRectMake(5, 5, bookCover.frame.size.width -10, bookCover.frame.size.height - 10)];
    [bookCoverName setText:timelineModel.bookName fitToContent:YES];
     bookCoverName.verticalAlignment = VerticalAlignmentMiddle;
    bookCoverName.textAlignment = NSTextAlignmentCenter;
    [bookCoverName setTextColor:[UIColor whiteColor]];
    bookCoverName.font = [UIFont systemFontOfSize:12];
    [bookCover addSubview:bookCoverName];
    
    UIImage *image = [self makeImageWithView:bookCover];
    
    // 图书 图片
   // [self.bookImageView sd_setImageWithURL:[NSURL URLWithString:timelineModel.bookImage]];
    [self.bookImageView sd_setImageWithURL:[NSURL URLWithString:timelineModel.bookImage] placeholderImage:image];
    
    [self.bookImageView.layer setBorderColor:[UIColor whiteColor].CGColor];
    [self.bookImageView.layer setBorderWidth:3];
    
    // 图书名
    NSString *bookName = timelineModel.bookName;
    [self.bookNameLabel setText:bookName fitToContent:YES];
    self.bookNameLabel.verticalAlignment = VerticalAlignmentTop;
    self.bookNameLabel.textColor = [UIColor colorWithHexString:bookNameColor];
    // 借阅期
    NSString *borrowTime = NSLocalizedString(@"borrowTime", nil);
    NSString *str = [NSString stringWithFormat:@"%@: %@", borrowTime,timelineModel.borrowDuration];
    [self.borrowTimeLabel setText:str fitToContent:YES];
    
    // 持有人
    NSString *bookSourceStr = NSLocalizedString(@"bookSource", nil);
    NSString *bookSource = [NSString stringWithFormat:@"%@: %@", bookSourceStr,timelineModel.bookSource];
    [self.lenderLabel setText:bookSource fitToContent:YES];
    
    // 状态图片
    //self.stateImageView.backgroundColor = [UIColor colorWithHexString:@"#BFBFBF"];
    self.stateImageView.image = [UIImage imageNamed:@"book@3x"];
    //self.stateImageView.layer.cornerRadius = 10;
    self.stateView.layer.cornerRadius = 10;
    self.stateView.backgroundColor = [UIColor supportGreenColor];
    
    self.borrowTimeLabel.textColor = [UIColor colorWithHexString:borrowTimeColor];
    self.lenderLabel.textColor = [UIColor colorWithHexString:lenderColor];
    self.borrowTimeLabel.font = [UIFont systemFontOfSize:12];
    self.lenderLabel.font = [UIFont systemFontOfSize:12];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickBookImage:)];
    [self.bookImageView addGestureRecognizer:tap];
    self.bookImageView.userInteractionEnabled = YES;

    // 给头像添加手势方法
    UITapGestureRecognizer *userImageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickUserImage:)];
    self.userImageView.userInteractionEnabled = YES;
    [self.userImageView addGestureRecognizer:userImageTap];
    
    if (timelineModel.isReplyCommentAllShow) {
        self.commentView.sumHeight = timelineModel.rowHeight;
    }else{
        self.commentView.sumHeight = timelineModel.beforeTenRowHeight;
    }
        self.commentView.heightArray = timelineModel.replyHeightArray;
        self.commentView.isShowAllReply = timelineModel.isReplyCommentAllShow;
        self.commentView.isClickMoreDataButton = YES;
        [self.commentView transferCommetArray:timelineModel.replyArray indexPath:self.borrowCellIndexPath];
}
#pragma mark - 点击头像触发的方法
- (void)clickUserImage:(UITapGestureRecognizer *)tap{
    NSLog(@"borrow 点了头像");
    [self.delegate borrowCellTurnPersonalPageWithUserID:self.timelineModel.userID];
}
- (void)setBorrowCellIndexPath:(NSIndexPath *)borrowCellIndexPath{
    if (_borrowCellIndexPath != borrowCellIndexPath) {
        _borrowCellIndexPath = borrowCellIndexPath;
    }
}

#pragma mark - 手势触发的方法
- (void)clickBookImage:(UITapGestureRecognizer *)tap{
    [self.delegate borrowBookShowDetail:self.timelineModel.bookID];
}



#pragma mark - 计算文本高度
- (CGFloat)heightWithText:(NSString *)text Width:(CGFloat)width Font:(CGFloat)font{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:font]};
    CGSize size = CGSizeMake(width, 5000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    /** 返回得到的高度 */
    return rect.size.height;
}



@end
